//
//  NewsDetailViewController.m
//  B
//
//  Created by lanou on 16/5/11.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "SVProgressHUD.h"
#import "UIView+QHExtension.h"
#import "DBTools.h"
#import "CommentViewController.h"

@interface NewsDetailViewController ()

@property (nonatomic, strong) UIWebView *web;

@property (nonatomic, strong) UIButton *collectionButton;

@end

@implementation NewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBarController.tabBar.hidden = YES;
    [SVProgressHUD showWithStatus:@"waiting"];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64 - 44, ScreenWidth, ScreenHeight)];
    NSURL *url = [NSURL URLWithString:self.getUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.web loadRequest:request];
    [self.view addSubview:self.web];
    [self createButtons];
    NSLog(@"%@", self.number);
}

-(void)viewDidAppear:(BOOL)animated
{
    [SVProgressHUD dismiss];
}

-(void)createButtons
{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - 50, ScreenWidth, 50)];
    backView.backgroundColor = [UIColor grayColor];;
    
    // 收藏
    self.collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectionButton.frame = CGRectMake(backView.frame.size.width / 4 - 20, backView.frame.size.height / 2 - 20, 40, 40);
    [self.collectionButton setImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
    [self.collectionButton addTarget:self action:@selector(shoucang:) forControlEvents:UIControlEventTouchUpInside];
    // 缩放
    UIButton *smallButton = [UIButton buttonWithType:UIButtonTypeCustom];
    smallButton.selected = NO;
    smallButton.frame = CGRectMake(backView.frame.size.width / 4 * 2  - 20, backView.frame.size.height / 2 - 20, 40, 40);
    [smallButton setImage:[UIImage imageNamed:@"small"] forState:UIControlStateNormal];
    [smallButton addTarget:self action:@selector(suofang:) forControlEvents:UIControlEventTouchUpInside];
    smallButton.tag = 100000;
    // 放大
    UIButton *bigButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bigButton.selected = NO;
    bigButton.frame = CGRectMake(backView.frame.size.width / 4 * 3 - 20, backView.frame.size.height / 2 - 20, 40, 40);
    [bigButton setImage:[UIImage imageNamed:@"big"] forState:UIControlStateNormal];
    [bigButton addTarget:self action:@selector(suofang:) forControlEvents:UIControlEventTouchUpInside];
    bigButton.tag = 100001;
    
    [backView addSubview:self.collectionButton];
    [backView addSubview:smallButton];
    [backView addSubview:bigButton];
    [self.view addSubview:backView];
}

#pragma mark - 按钮方法
// 收藏
-(void)shoucang:(UIButton *)button
{
    if ([[DBTools sharedTools] addReadCollectionWithModel:self.model]) {
        NSLog(@"收藏成功");
        [self.view popMessageWithTitle:@"收藏成功" postion:(CENTER)];
        self.collectionButton.selected = YES;
        [self.collectionButton setImage:[UIImage imageNamed:@"shoucangOK"] forState:UIControlStateSelected];
    } else {
        // 如果返回NO，说明已经存在这条数据，将它删除并提示取消收藏
        [[DBTools sharedTools]deleteReadCollectionWithModel:self.model];
        self.collectionButton.selected = NO;
        [self.collectionButton setImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
        [self.view popMessageWithTitle:@"取消收藏" postion:(CENTER)];
    }
}

// 缩放
-(void)suofang:(UIButton *)button
{
    UIWebView *webView = self.web;
    
    if (button.tag == 100000) { // 缩放
        if (button.selected == YES) {
            button.selected = NO;
            [self.view popMessageWithTitle:@"已经最小" postion:(CENTER)];
        }
        [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 0.8"];
        button.selected = YES;
    } else {
        [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 1.2"];
        if (button.selected == YES) {
            [self.view popMessageWithTitle:@"已经最大" postion:(CENTER)];
        }
        button.selected = YES;
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark - 代理


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
